package com.jia.config;

import com.jia.service.MyUserService;
import com.jia.service.impl.MyUserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;

/**
 * @author 贾宇光
 * @version 1.0
 * @date 2020/9/16 21:57
 */
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private MyUserServiceImpl myUserService;

    //授权
    @Override
    protected void configure(HttpSecurity http) throws Exception {
        //首页谁都可以访问
        //功能项只能有对应权限的人才能访问
        http.authorizeRequests()
                .antMatchers("/", "/index").permitAll()
                .antMatchers("/myuser/select").hasAuthority("MYUSER_VIEW")
                .antMatchers("/myuser/add").hasAuthority("MYUSER_ADD")
                .antMatchers("/myuser/addData").hasAuthority("MYUSER_ADD")
                .antMatchers("/myuser/update/**").hasAuthority("MYUSER_UPDATE")
                .antMatchers("/myuser/updateData").hasAuthority("MYUSER_UPDATE")
                .antMatchers("/myuser/delete/**").hasAuthority("MYUSER_DELETE")
                .antMatchers("/role/select").hasAuthority("ROLE_VIEW")
                .antMatchers("/role/add").hasAuthority("ROLE_ADD")
                .antMatchers("/role/addData").hasAuthority("ROLE_ADD")
                .antMatchers("/role/update").hasAuthority("ROLE_UPDATE")
                .antMatchers("/role/updateData").hasAuthority("ROLE_UPDATE")
                .antMatchers("/role/delete/**").hasAuthority("ROLE_DELETE")
                .antMatchers("/authority/select").hasAuthority("AUTHORITY_VIEW")
                .antMatchers("/authority/add").hasAuthority("AUTHORITY_ADD")
                .antMatchers("/authority/addData").hasAuthority("AUTHORITY_ADD")
                .antMatchers("/authority/update/**").hasAuthority("AUTHORITY_UPDATE")
                .antMatchers("/authority/update").hasAuthority("AUTHORITY_UPDATE")
                .antMatchers("/authority/delete/**").hasAuthority("AUTHORITY_DELETE");
        //没有权限会开启登录页
        //定制登录页
        //自定义接收前端的参数
        http.formLogin()
                .loginPage("/toLogin")
                .loginProcessingUrl("/loginMark")
                .usernameParameter("username")
                .passwordParameter("password");

        //注销 跳到首页
        http.logout().logoutSuccessUrl("/");
        //关闭csrf功能
        http.csrf().disable();

        //开启 "记住我" 功能 cookie 14天有效
        //自定义接收前端的参数
        http.rememberMe().rememberMeParameter("rememberMe");
    }

    // 认证
    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(myUserService).passwordEncoder(passwordEncoder());

    }

    /**
     * 密码加密
     */
    @Bean
    public BCryptPasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

